<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row>
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="small"
                @click="showDialog = true"
              >
                新增角色
              </el-button>
            </el-row>
            <el-table
              :data="tableData"
              style="width: 100%"
            >
              <el-table-column
                align="center"
                type="index"
                label="序号"
                width="120"
              />
              <el-table-column
                align="center"
                prop="name"
                label="角色名称"
                width="240"
              />
              <el-table-column
                align="center"
                prop="description"
                label="描述"
              />
              <el-table-column
                align="center"
                label="操作"
              >
                <template slot-scope="{row}">

                  <el-button type="success" size="small">分配权限</el-button>
                  <el-button type="primary" size="small" @click="editRoles(row.id)">编辑</el-button>
                  <el-button type="danger" size="small" @click="delRoles(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle">
              <el-pagination
                layout="prev, pager, next"
                :page-size="page.pagesize"
                :total="page.total"
                :current-page="page.page"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.name" disabled style="width:400px" />

              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />

              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />

              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" disabled style="width:400px" />

              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog title="编辑角色" :visible="showDialog" @close="cancelBtn">
        <el-form ref="roleForm" :model="roleForm" :rules="roleRules" label-width="120px">
          <el-form-item prop="name" label="角色名称">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item prop="description" label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="4">
            <el-button size="small" @click="cancelBtn">取消</el-button>
            <el-button size="small" type="primary" @click="confirmBtns">确定</el-button>
          </el-col>
        </el-row>

      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAllRole, getCompanyId, delRole, getRole, updateRole, addRole } from '@/api/company'
import { mapGetters } from 'vuex'
export default {
  props: {

  },

  data() {
    return {
      tableData: [],
      page: {
        page: 1,
        pagesize: 3,
        total: 0
      },

      companyForm: {
      },
      roleForm: {
        name: '',
        description: ''
      },
      showDialog: false,
      roleRules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getAllRoles()
    this.getCompanys()
  },
  methods: {
    //   得到所有角色
    async getAllRoles() {
      const result = await getAllRole(
        this.page
      )
      this.page.total = result.total
      this.tableData = result.rows
    },
    // 分页
    currentChange(newPage) {
      this.getAllRoles()
      this.page.page = newPage
    },
    // 根据id查询企业信息
    async getCompanys() {
      const result = await getCompanyId(this.companyId)
      this.companyForm = result
    },
    // 删除角色
    async delRoles(id) {
      try {
        await confirm('确定删除吗')
        await delRole(id)
        this.getAllRoles()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 编辑角色
    async editRoles(id) {
      this.roleForm = await getRole(id)
      this.showDialog = true
    },
    // 更新角色
    async confirmBtns() {
      try {
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        this.getAllRoles()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (err) {
        console.log(err)
      }
    },
    // 点击取消
    cancelBtn() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    }

  }
}
</script>

<style>

</style>
